part of "../basic.dart";

/// 循环特定的次数，由于每一次都是需要自己临时想for循环的方法，不如一次性将它固定下来
/// 在执行的函数中，会将当前是loop第几次作为信息传给fn
loopSync(int number, void Function(int order) fn) {
  for (int order = 1; order <= number; order++) {
    fn(order);
  }
}

Future<void> loop(int number, Future<void> Function(int order) fn) async {
  for (int order = 1; order <= number; order++) {
    await fn(order);
  }
}

/// 会先运行一次，然后每过一段时间检查条件，当条件满足时会一直执行
void runAndLoop({
  required Function() fn,
  required Duration interval,
  required bool Function() condition,
}) {
  fn();
  if (condition()) {
    Future.delayed(
      interval,
      () => runAndLoop(
        condition: condition,
        interval: interval,
        fn: fn,
      ),
    );
  }
}
